﻿<%@ WebHandler Language="C#" Class="DocHandler" %>

using System;
using System.Text;
using System.Web;
using DocumentBuilder;

public class DocHandler : IHttpHandler
{
    private const string ScriptFormat = @"<script src='{0}' type='text/javascript' language='javascript'></script>";
    private const string CssFormat = @"<link href='{0}' type='text/css' rel='stylesheet'></script>";
    private const string DDFormat = "Fingerbrisk.components.addComponent(Fingerbrisk.components.type.document, {0} \"data\":{1}, \"callback\": {2}, \"context\": {3} {4});";
    private const string PageFormat = @"<html>
    <title>
        {0}
    </title>
    <head>
        <!-- css -->
        {1}
        <!-- scripts -->
        {2}
    </head>
    <body>
            {3}
        <!-- inline scripts -->
        <script type='text/javascript' language='javascript'>
            {4}
        </script>
    </body>
</html>";
    
    public void ProcessRequest (HttpContext context)
    {
        var docPath = context.Request.QueryString["_path"];
        var jsCallback = context.Request.QueryString["_scb"];
        var luid = context.Request.QueryString["_luid"];
        if (String.IsNullOrEmpty(docPath))
        {
            context.Response.Write("Hello World");
            return;
        }

        docPath = docPath + ".xml";
        var doc = new Document();
        doc.Load(HttpContext.Current.Server.MapPath(docPath), null);

        if (String.IsNullOrEmpty(jsCallback))
        {
            var scriptBuilder = new StringBuilder("");
            var cssBuilder = new StringBuilder("");
            var documentBuilder = new StringBuilder("");
            
            foreach (var script in doc.View.Scripts)
            {
                scriptBuilder.AppendLine(String.Format(ScriptFormat, script));
            }

            foreach (var css in doc.View.Css)
            {
                cssBuilder.AppendLine(String.Format(CssFormat, css));
            }

            foreach (var path in doc.DependentDocuments.Keys)
            {
                foreach (var docContext in doc.DependentDocuments[path])
                {
                    var tempContext = new StringBuilder();
                    tempContext.Append("{");
                    tempContext.Append(String.Format("\"docPath\": \"{0}\", ", path));
                    docContext.PaintJsonString(tempContext);
                    tempContext.Append("}");
                    documentBuilder.AppendLine(String.Format(DDFormat, "{", tempContext.ToString(), "Fingerbrisk.docResponse.onSuccess", "null", "}"));
                }
            }

            try
            {
                var html = String.Format(PageFormat, "", cssBuilder.ToString(), scriptBuilder.ToString(), doc.View.Html, documentBuilder.ToString());
                context.Response.Write(html);
                return;
            } catch
            {
                context.Response.Write("An unexpexected error occurred while processing request");
            }
        }
        else
        {
            var canvas = new StringBuilder();
            doc.PaintJsonString(canvas);
            context.Response.Write("new " + jsCallback + "({ " + String.Format("\"luid\": {0}, \"document\": {1} ", luid, canvas.ToString()) + "});");
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}